package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zk2/internal/dto"
	"zk2/internal/service"
)

type FriendHandler struct {
	FriendService service.FriendServiceImpl
}

func NewFriendHandler() *FriendHandler {
	return &FriendHandler{
		service.NewFriendService(),
	}
}

func (f *FriendHandler) FriendAdd(c *gin.Context) {
	var friendReq dto.FriendReq
	if err := c.ShouldBindJSON(&friendReq); err != nil {
		// 返回错误信息
		// gin.H封装了生成json数据的工具
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	add, err := f.FriendService.FriendAdd(friendReq)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "好友申请发送失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "好友申请发送成功",
		"data": add,
	})
	return
}
